<?php
namespace M\Controller;
use Think\Controller;

class PasswordapiController extends Controller{
    public function _initialize(){
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET');
        header('Access-Control-Allow-Methods:POST');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
    }

    /**修改密码
     * para:user_id
     * return:
     *  code
     *  msg
     */
    public function update(){
        $id=I('user_id');
        $phonenumber=I('phonenumber');
        $password=I('password');
        $newpassword=I('newpassword');
        $user=M('user');
        $result=$user->where(array('phonenumber'=>$phonenumber))->field('id,password')->find();
        if($result!='' && md5($password)==$result['password'])
        {
            $user->password=md5($newpassword);
            $flag=$user->where(array('phonenumber'=>$phonenumber))->save();
            if($flag!=false)
            {
                $data=array(
                    'code'=>'1',
                    'msg'=>'密码成功',
                );
            }else{
                $data=array(
                    'code'=>'2',
                    'msg'=>'新密码不能跟原密码相同',
                );
            }
        }else{
            $data=array(
                'code'=>'0',
                'msg'=>'原有密码或电话号码有误'
            );
        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }
}